﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;


namespace BuildSlip
{
    public partial class Form1 : Form
    {
        const string author = "Author: Jack A. Coneybeer - 4/15/2011";

        public Form1()
        {
            InitializeComponent();        
        }

        private void Process()
        {
            string RTMFolder;
            string x86SPFile;
            string x86CUFile;
            string x64SPFile;
            string x64CUFile;
            string DestFolder;
            string Command;

            // Collect directory information 

            FolderDialog.Description = " Enter Folder containing SQL2008 RTM Files";
            FolderDialog.ShowNewFolderButton = false;
            FolderDialog.ShowDialog();
            RTMFolder = FolderDialog.SelectedPath;

            FolderDialog.Description = " Enter Folder for generated SlipStream files";
            FolderDialog.ShowNewFolderButton = false;
            FolderDialog.ShowDialog();
            DestFolder = FolderDialog.SelectedPath;

            if (RTMFolder == DestFolder)
                throw new DupException();

            // Extract service pack and cumulative update files to working directory


            if (x86.Checked == true)
            {
                FileDialog.Title = " Enter x86 Service Pack file to be extracted";
                FileDialog.ShowDialog();
                x86SPFile = FileDialog.FileName;
                Extract(x86SPFile, DestFolder + @"\PCU");
            }

            if (x64.Checked == true)
            {
                FileDialog.Title = " Enter x64 Service Pack file to be extracted";
                FileDialog.ShowDialog();
                x64SPFile = FileDialog.FileName;
                Extract(x64SPFile, DestFolder + @"\PCU");
            }

            if (x86.Checked == true & SPandCU.Checked == true)
            {
                FileDialog.Title = " Enter x86 Cumulative Update file to be extracted";
                FileDialog.ShowDialog();
                x86CUFile = FileDialog.FileName;
                Extract(x86CUFile, DestFolder + @"\CU");
            }

            if (x64.Checked == true & SPandCU.Checked == true)
            {
                FileDialog.Title = " Enter x64 Cumulative Update file to be extracted";
                FileDialog.ShowDialog();
                x64CUFile = FileDialog.FileName;
                Extract(x64CUFile, DestFolder + @"\CU");
            }

            // Copy RTM files to SlipStream Destination Directory 
            // XCopy was used to ensure availability of command

            Command = @"/D /C Xcopy " + @"""" + RTMFolder + @""" " + @"""" + DestFolder + @"""" + @" /S /Y";
            ExecCmd(Command);

            if (SPOnly.Checked == true)
            {
                if (x86.Checked == true)
                {
                    //copy in SQLSupport.msi from the Service Pack
                    Command = @"/D /C Xcopy " + @"""" + DestFolder + @"\PCU\x86\setup\1033\sqlsupport.msi" + @""" " +
                                  @"""" + DestFolder + @"\x86\setup""" + " /I /Y";
                    ExecCmd(Command);
                    //copy in Service Pack files
                    Command = @"/D /C Xcopy " + @"""" + DestFolder + @"\PCU\x86\*.*" + @""" " +
                                  @"""" + DestFolder + @"\x86""" + " /I /Y";
                    ExecCmd(Command);
                    // put back the Microsoft.SQL.Chainer.PackageData.dll
                    Command = @"/D /C Xcopy " + @"""" + RTMFolder + @"\x86\Microsoft.SQL.Chainer.PackageData.dll" + @""" " +
                                          @"""" + DestFolder + @"\x86\""" + " /I /Y";
                    ExecCmd(Command);
                }

                if (x64.Checked == true)
                {
                    //copy in SQLSupport.msi from the Service Pack
                    Command = @"/D /C Xcopy " + @"""" + DestFolder + @"\PCU\x64\setup\1033\sqlsupport.msi" + @""" " +
                                  @"""" + DestFolder + @"\x64\setup""" + " /I /Y";
                    ExecCmd(Command);
                    //copy in Service Pack files
                    Command = @"/D /C Xcopy " + @"""" + DestFolder + @"\PCU\x64\*.*" + @""" " +
                                  @"""" + DestFolder + @"\x64""" + " /I /Y";
                    ExecCmd(Command);
                    // put back the Microsoft.SQL.Chainer.PackageData.dll
                    Command = @"/D /C Xcopy " + @"""" + RTMFolder + @"\x64\Microsoft.SQL.Chainer.PackageData.dll" + @""" " +
                                          @"""" + DestFolder + @"\x64\""" + " /I /Y";
                    ExecCmd(Command);
                }

                //copy in setup files 

                Command = @"/D /C Xcopy " + @"""" + DestFolder + @"\PCU\setup.exe" + @""" " +
                                  @"""" + DestFolder + @"""" + " /I /Y";
                ExecCmd(Command);
                Command = @"/D /C Xcopy " + @"""" + DestFolder + @"\PCU\setup.rll" + @""" " +
                                  @"""" + DestFolder + @"""" + " /I /Y";
                ExecCmd(Command);


            }

            if (SPandCU.Checked == true)
            {
                if (x86.Checked == true)
                {
                    //copy in SQLSupport.msi from the Service Pack
                    Command = @"/D /C Xcopy " + @"""" + DestFolder + @"\CU\x86\setup\1033\sqlsupport.msi" + @""" " +
                                  @"""" + DestFolder + @"\x86\setup""" + " /I /Y";
                    ExecCmd(Command);
                    //copy in Cumulative Update files
                    Command = @"/D /C Xcopy " + @"""" + DestFolder + @"\CU\x86\*.*" + @""" " +
                                  @"""" + DestFolder + @"\x86""" + " /I /Y";
                    ExecCmd(Command);
                    // put back the Microsoft.SQL.Chainer.PackageData.dll
                    Command = @"/D /C Xcopy " + @"""" + RTMFolder + @"\x86\Microsoft.SQL.Chainer.PackageData.dll" + @""" " +
                                          @"""" + DestFolder + @"\x86\""" + " /I /Y";
                    ExecCmd(Command);
                }

                if (x64.Checked == true)
                {
                    //copy in SQLSupport.msi from the Service Pack
                    Command = @"/D /C Xcopy " + @"""" + DestFolder + @"\CU\x64\setup\1033\sqlsupport.msi" + @""" " +
                                  @"""" + DestFolder + @"\x64\setup""" + " /I /Y";
                    ExecCmd(Command);
                    //copy in Cumulative Update files
                    Command = @"/D /C Xcopy " + @"""" + DestFolder + @"\CU\x64\*.*" + @""" " +
                                  @"""" + DestFolder + @"\x64""" + " /I /Y";
                    ExecCmd(Command);
                    // put back the Microsoft.SQL.Chainer.PackageData.dll 
                    Command = @"/D /C Xcopy " + @"""" + RTMFolder + @"\x64\Microsoft.SQL.Chainer.PackageData.dll" + @""" " +
                                          @"""" + DestFolder + @"\x64\""" + " /I /Y";
                    ExecCmd(Command);
                }
                //copy in setup files 

                Command = @"/D /C Xcopy " + @"""" + DestFolder + @"\CU\setup.exe" + @""" " +
                                  @"""" + DestFolder + @"""" + " /I /Y";
                ExecCmd(Command);
                Command = @"/D /C Xcopy " + @"""" + DestFolder + @"\CU\setup.rll" + @""" " +
                                  @"""" + DestFolder + @"""" + " /I /Y";
                ExecCmd(Command);

            }

            //  Erase non-selected architecture files

            if (x86.Checked == false)
            {
                Command = @"/D /C rd " + @"""" + DestFolder + @"\x86" + @""" " + " /S /Q";
                ExecCmd(Command);
                Command = @"/D /C rd " + @"""" + DestFolder + @"\PCU\x86" + @""" " + " /S /Q";
                ExecCmd(Command);
                Command = @"/D /C rd " + @"""" + DestFolder + @"\CU\x86" + @""" " + " /S /Q";
                ExecCmd(Command);
            }

            if (x64.Checked == false)
            {
                Command = @"/D /C rd " + @"""" + DestFolder + @"\x64" + @""" " + " /S /Q";
                ExecCmd(Command);
                Command = @"/D /C rd " + @"""" + DestFolder + @"\PCU\x64" + @""" " + " /S /Q";
                ExecCmd(Command);
                Command = @"/D /C rd " + @"""" + DestFolder + @"\CU\x64" + @""" " + " /S /Q";
                ExecCmd(Command);
            }                     
               
        }
        

           private int Extract(string FileName, string Destination)
           {
            // Function for extracting service pack and cumulative update files
             
            string exeName;
            string argsLine;
            exeName = FileName;
            argsLine = @" /x:" + @"""" + Destination + @"""";
            Process newProcess = new Process();
            newProcess.StartInfo.FileName = exeName;
            newProcess.StartInfo.Arguments = argsLine;
            newProcess.StartInfo.UseShellExecute = false;
            newProcess.StartInfo.CreateNoWindow = true;
            newProcess.StartInfo.RedirectStandardError = false;
            newProcess.StartInfo.RedirectStandardOutput = false;

            newProcess.Start();
            newProcess.WaitForExit();
            int exitcode = newProcess.ExitCode;
            newProcess.Close();
            return exitcode;
           }

            private int ExecCmd (string Command)
            {
                // Function for executing command line commands

            string exeName;
            string argsLine;          
            exeName = @"cmd.exe";  
            argsLine = Command;
            WaitMessage.Visible = true;

            Process newProcess = new Process();
            newProcess.StartInfo.FileName = exeName;
            newProcess.StartInfo.Arguments = argsLine;
            newProcess.StartInfo.UseShellExecute = false;
            newProcess.StartInfo.CreateNoWindow = true;
            newProcess.StartInfo.RedirectStandardError = false;
            newProcess.StartInfo.RedirectStandardOutput = false;

            newProcess.Start();
            newProcess.WaitForExit();         
            int exitcode = newProcess.ExitCode;            
            newProcess.Close();
            WaitMessage.Visible = false;          
            return exitcode;
            }

            private void Generate_Click(object sender, EventArgs e)
            {
                Completed.Visible = false;
                WaitMessage.Visible = false;
                Failure.Visible = false;
                DupError.Visible = false;

                try
                {
                    Process();
                    Completed.Visible = true;
                }
                catch (DupException)
                {
                    DupError.Visible = true;
                }
                catch
                {
                    Failure.Visible = true;
                }         

            }

            private void Form1_Load(object sender, EventArgs e)
            {

            }
            public class DupException : Exception
            {

            }

            
           
        
    }
}
